<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="isLoding" class="tree-card">
        <!-- 用了一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>{{ listName }}</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex">
              <!-- 两个内容 -->
              <el-col :span="12">负责人</el-col>
              <el-col :span="12">
                <!-- 下拉菜单 element -->
                <el-dropdown @command="hdremoveDialog">
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <!-- 下拉菜单 -->
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item :command="{type:'add'}">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="departs" :default-expand-all="true">
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex">
                  <!-- 两个内容 -->
                  <el-col :span="12">{{ data.manager }}</el-col>
                  <el-col :span="12">
                    <!-- 下拉菜单 element -->
                    <el-dropdown @command="handleCommand">
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item :command="{type:'add',data}">添加子部门</el-dropdown-item>
                        <el-dropdown-item :command="{type:'edit',id:data.id,data}">修改部门</el-dropdown-item>
                        <el-dropdown-item :command="{type:'del',id:data.id}">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 添加弹出框 -->
    <addDept ref="son" :show-dialog.sync="isShow" :son-data="sonData" :son-data-list="sonDataList" :type="type" @btn="getDepartmentsList" />
    <addDepts ref="hdson" :hdshow-dialog.sync="hdisShow" :hd-data="sonDataList" @hdadd="getDepartmentsList" />
  </div>
</template>

<script>
import { getDepartmentsApi, delDepartment } from '@/api/departments'
import { scree } from '@/utils'
import { Message } from 'element-ui'
import addDept from './components/add-dept.vue'
import addDepts from './components/add-depts.vue'
export default {
  name: 'Departments',
  components: {
    addDept,
    addDepts
  },
  data() {
    return {
      listName: '',
      departs: [],
      isShow: false,
      hdisShow: false,
      // 传给下子组件 用于判断部门名称是否存在重复
      sonData: {},
      // 传给下子组件 用于判断部门编号是否存在重复
      sonDataList: [],
      // 模态框是谁点击的
      type: '',
      // 加载状态
      isLoding: false
    }
  },
  created() {
    this.getDepartmentsList()
  },
  methods: {
    /**
     * 获取组织架构列表数据
     */
    async  getDepartmentsList() {
      this.isLoding = true
      const { data } = await getDepartmentsApi()
      this.sonDataList = JSON.parse(JSON.stringify(data.depts))
      this.listName = data.companyName
      // console.log(this.sonDataList)
      this.departs = scree(data.depts, '')
      this.isLoding = false
    },
    /**
     * 操作区域下拉选择框按钮
     */
    handleCommand({ type, id, data }) {
      if (type === 'add') {
        // 添加功能
        this.removeDialog(data)
        // 通过父组件调用子组件中的方法 获取负责人名单
        this.$refs.son.getDepartmentUserList()
        this.type = type
      }
      if (type === 'edit') {
        // 修改功能
        this.editDepartment(id)
        this.type = type
      }
      if (type === 'del') {
        // 删除功能
        this.delDepartment(id)
      }
    },
    editDepartment(id) {
      this.isShow = true
      // 父组件调用子组件的方法 回填数据
      this.$refs.son.getFormData(id)
      // 通过父组件调用子组件中的方法 获取负责人名单
      this.$refs.son.getDepartmentUserList()
    },
    /**
     * 下方子分支显示模态框
     */
    removeDialog(data) {
      this.isShow = true
      this.sonData = data
      // console.log(this.sonData)
    },
    /**
     * 上方主分支显示模态框
     */
    hdremoveDialog() {
      this.hdisShow = true
      // 调用子组件中的方法
      this.$refs.hdson.getDepartmentUserList()
    },
    /**
     * 删除功能
     */
    delDepartment(id) {
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示').then(async() => {
        // 1.调用接口 删除
        await delDepartment(id)
        // 2.删除成功提示
        Message.success('删除成功')
        // 3.从新请求数据渲染页面
        this.getDepartmentsList()
      }).catch(() => {

      })
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
